package com.zixi.beverage_order.service.customer;

import com.zixi.beverage_order.mapper.OrderListMapper;
import com.zixi.beverage_order.mapper.OrderedProductMapper;
import com.zixi.beverage_order.mapper.ProductMapper;
import com.zixi.beverage_order.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxOrderListServiceImpl implements WxOrderListService {
    @Autowired
    OrderListMapper orderListMapper;

    @Autowired
    OrderedProductMapper orderedProductMapper;

    @Autowired
    ProductMapper productMapper;

    @Override
    public ReturnOrderList newOrderList(OrderList newOrderList) {
        orderListMapper.insert(newOrderList);
        ReturnOrderList returnOrderList = new ReturnOrderList(newOrderList.getId().toString(), newOrderList.getStarttime(), newOrderList.getFinishtime(), newOrderList.getRemark(), newOrderList.getCustomerId(), newOrderList.getAddress(), newOrderList.getPacked());
        System.out.println("经过数据库后的orderList对象:" + returnOrderList);
        System.out.println("经过数据库后的orderListID的类型:" + returnOrderList.getId().getClass());
        return returnOrderList;
    }

    @Override
    public ShoppingTrolley wxGetshoppingtrolley(OrderList orderList) {
        Map<String, Object> mbpMap = new HashMap<>();
        mbpMap.put("order_list_id", orderList.getId());
        List<OrderedProduct> orderedProducts = orderedProductMapper.selectByMap(mbpMap);
        List<ReturnOrderedProduct> returnOrderedProducts = new ArrayList<>();
        for (OrderedProduct n: orderedProducts){
            ReturnOrderedProduct returnOrderedProduct = new ReturnOrderedProduct(n.getId().toString(),n.getOrderListId().toString(),n.getProductId(),
                    n.getTemperature(),n.getSweetness());
            returnOrderedProducts.add(returnOrderedProduct);
        }
        OrderList orderListReturn = orderListMapper.selectById(orderList.getId());
        ShoppingTrolley shoppingTrolley = new ShoppingTrolley(orderListReturn.getId().toString(), null,
               null, orderListReturn.getRemark(), orderListReturn.getCustomerId(), orderListReturn.getAddress()==null?null:orderListReturn.getAddress(), orderListReturn.getPacked()?"打包":"不打包", 0,returnOrderedProducts, null);
        List<ReturnOrderedProductInfo> returnOrderedProductInfoList = new ArrayList<>();
        Integer sum = 0;
        for (OrderedProduct n : orderedProducts) {
            ReturnOrderedProductInfo returnOrderedProductInfo = new ReturnOrderedProductInfo();
            //写入产品名
            returnOrderedProductInfo.setProductName(productMapper.selectById(n.getProductId()).getName());
            //写入温度
            if (n.getTemperature() == 2) {
                returnOrderedProductInfo.setTemperature("烫热");
            } else if (n.getTemperature() == 1) {
                returnOrderedProductInfo.setTemperature("温热");
            } else if (n.getTemperature() == 0) {
                returnOrderedProductInfo.setTemperature("常温");
            } else if (n.getTemperature() == -1) {
                returnOrderedProductInfo.setTemperature("去冰");
            } else if (n.getTemperature() == -2) {
                returnOrderedProductInfo.setTemperature("少冰");
            } else if (n.getTemperature() == -3) {
                returnOrderedProductInfo.setTemperature("正常冰");
            }
            if (n.getSweetness() == 0) {
                returnOrderedProductInfo.setSweetness("无糖");
            } else if (n.getSweetness() == 1) {
                returnOrderedProductInfo.setSweetness("微糖");
            } else if (n.getSweetness() == 2) {
                returnOrderedProductInfo.setSweetness("半糖");
            } else if (n.getSweetness() == 3) {
                returnOrderedProductInfo.setSweetness("少糖");
            } else if (n.getSweetness() == 4) {
                returnOrderedProductInfo.setSweetness("全糖");
            }
            sum += productMapper.selectById(n.getProductId()).getPrice();
            returnOrderedProductInfoList.add(returnOrderedProductInfo);
        }
        shoppingTrolley.setTotalPrice(sum);
        shoppingTrolley.setProducts(returnOrderedProductInfoList);
        return shoppingTrolley;
    }

    @Override
    public List<ReturnOrderList> getUnfinishedList(Customer customer) {
//        System.out.println("unfinishedlist"+customer);
        Map<String, Object> map = new HashMap<>();
        map.put("customer_id", customer.getId());
        map.put("finishtime", null);
        List<ReturnOrderList> returnUnfinishedList = new ArrayList<>();
        for (OrderList n : orderListMapper.selectByMap(map)) {
            ReturnOrderList returnOrderList = new ReturnOrderList(n.getId().toString(), n.getStarttime(), n.getFinishtime(), n.getRemark(), n.getCustomerId(), n.getAddress(), n.getPacked());
            returnUnfinishedList.add(returnOrderList);
        }
        return returnUnfinishedList;
    }

    @Override
    public List<ReturnOrderList> getFinishedList(Customer customer) {
        Map<String, Object> map = new HashMap<>();
        map.put("customer_id", customer.getId());
        List<OrderList> allList = orderListMapper.selectByMap(map);
        List<OrderList> finishedList = new ArrayList<>();
        for (OrderList n : allList) {
            if (n.getFinishtime() != null) {
                finishedList.add(n);
            }
        }
        List<ReturnOrderList> returnFinishedList = new ArrayList<>();
        for (OrderList n : finishedList) {
            ReturnOrderList returnOrderList = new ReturnOrderList(n.getId().toString(), n.getStarttime(), n.getFinishtime(), n.getRemark(), n.getCustomerId(), n.getAddress(), n.getPacked());
            returnFinishedList.add(returnOrderList);
        }
        return returnFinishedList;
    }

    @Override
    public ShoppingTrolley getListInfo(OrderList orderList) {
        System.out.println(orderList);
        Map<String, Object> map = new HashMap<>();
        map.put("order_list_id", orderList.getId());
        List<OrderedProduct> orderedProducts = orderedProductMapper.selectByMap(map);
        List<ReturnOrderedProduct> returnOrderedProducts = new ArrayList<>();
        for (OrderedProduct n : orderedProducts) {
            returnOrderedProducts.add(new ReturnOrderedProduct(n.getId().toString(), n.getOrderListId().toString(), n.getProductId(), n.getTemperature(), n.getSweetness()));
        }
        OrderList orderList1 = orderListMapper.selectById(orderList.getId());
        ReturnOrderList returnOrderList = new ReturnOrderList(orderList1.getId().toString(), orderList1.getStarttime(), orderList1.getFinishtime(), orderList1.getRemark().toString(), orderList1.getCustomerId(), orderList1.getAddress(), orderList1.getPacked(), returnOrderedProducts);
        ShoppingTrolley shoppingTrolley = returnOrderList2ShoppingTrolley(returnOrderList);
        return shoppingTrolley;
    }

    private ShoppingTrolley returnOrderList2ShoppingTrolley(ReturnOrderList n){
        ShoppingTrolley shoppingTrolley = new ShoppingTrolley();
        shoppingTrolley.setId(n.getId());
        if (n.getStarttime() == null) {
            shoppingTrolley.setStarttime("无");
        } else {
            shoppingTrolley.setStarttime(n.getStarttime().toString());
        }
        if (n.getFinishtime() == null) {
            shoppingTrolley.setFinishtime("无");
        } else {
            shoppingTrolley.setFinishtime(n.getFinishtime().toString());
        }
        shoppingTrolley.setRemark(n.getRemark());
        shoppingTrolley.setCustomerId(n.getCustomerId());
        if (n.getAddress() == null) {
            shoppingTrolley.setAddress("无");
        } else {
            shoppingTrolley.setAddress(n.getAddress());
        }
        shoppingTrolley.setPacked(n.getPacked()?"打包":"不打包");
        shoppingTrolley.setReturnOrderedProducts(n.getOrderedProducts());
        List<ReturnOrderedProductInfo> returnOrderedProductInfoList = new ArrayList<>();
        Integer totalPrice = 0;
        for (ReturnOrderedProduct m: n.getOrderedProducts()){
            //每一个产品
            ReturnOrderedProductInfo returnOrderedProductInfo = new ReturnOrderedProductInfo();
            //写入产品名
            returnOrderedProductInfo.setProductName(productMapper.selectById(m.getProductId()).getName());
            //写入温度
            //extra Hot=2; Hot=1; normal0; no ice=-1; less ice=-2; iced=-3
            //no sugar=0; 25% sugar=1; 50% sugar=2; 75% sugar=3; 100% sugar=4
            if (m.getTemperature() == 2) {
                returnOrderedProductInfo.setTemperature("烫热");
            } else if (m.getTemperature() == 1) {
                returnOrderedProductInfo.setTemperature("温热");
            } else if (m.getTemperature() == 0) {
                returnOrderedProductInfo.setTemperature("常温");
            } else if (m.getTemperature() == -1) {
                returnOrderedProductInfo.setTemperature("去冰");
            } else if (m.getTemperature() == -2) {
                returnOrderedProductInfo.setTemperature("少冰");
            } else if (m.getTemperature() == -3) {
                returnOrderedProductInfo.setTemperature("正常冰");
            }
            if (m.getSweetness() == 0) {
                returnOrderedProductInfo.setSweetness("无糖");
            } else if (m.getSweetness() == 1) {
                returnOrderedProductInfo.setSweetness("微糖");
            } else if (m.getSweetness() == 2) {
                returnOrderedProductInfo.setSweetness("半糖");
            } else if (m.getSweetness() == 3) {
                returnOrderedProductInfo.setSweetness("少糖");
            } else if (m.getSweetness() == 4) {
                returnOrderedProductInfo.setSweetness("全糖");
            }
            returnOrderedProductInfoList.add(returnOrderedProductInfo);
            totalPrice += productMapper.selectById(m.getProductId()).getPrice();
        }
        shoppingTrolley.setProducts(returnOrderedProductInfoList);
        shoppingTrolley.setTotalPrice(totalPrice);
        return shoppingTrolley;
    }
}

